/**
 * 初始化详情对话框
 */
var ReNewInfoDlg = {
    reNewInfoData : {},
    canSubmit : true,
    validateFields: {
        insuranceId: {
            validators: {
                notEmpty: {
                    message: '保险公司不能为空'
                }
            }
        },
        effectiveDate: {
            validators: {
                notEmpty: {
                    message: '生效日期不能为空'
                },
                callback: {
                    message: '生效日期需早于失效日期',
                    callback: function() {
                        return $("#effectiveDate").val() <= $("#invalidDate").val();
                    }
                }
            }
        },
        policyCode: {
            validators: {
                notEmpty: {
                    message: '保单号不能为空'
                }
            }
        },
        invalidDate: {
            validators: {
                notEmpty: {
                    message: '失效日期不能为空'
                }
            }
        },
    }
};

/**
 * 清除数据
 */
ReNewInfoDlg.clearData = function() {
    this.reNewInfoData = {};
}
/**
 * 设置对话框中的数据
 *
 * @param key 数据的名称
 * @param val 数据的具体值
 */
ReNewInfoDlg.set = function(key, val) {
    this.reNewInfoData[key] = (typeof val == "undefined") ? $("#" + key).val() : val;
    return this;
}

/**
 * 设置对话框中的数据
 *
 * @param key 数据的名称
 * @param val 数据的具体值
 */
ReNewInfoDlg.get = function(key) {
    return $("#" + key).val();
}

/**
 * 关闭此对话框
 */
ReNewInfoDlg.close = function() {
    parent.layer.close(window.parent.Policy.layerIndex);
}

/**
 * 收集数据
 */
ReNewInfoDlg.collectData = function() {
    this
    .set('id')
    .set('insuranceId')
    .set('effectiveDate')
    .set('policyCode')
    .set('invalidDate');
}

/**
 * 验证数据是否为空
 */
ReNewInfoDlg.validate = function () {
    $('#reNewInfoForm').data("bootstrapValidator").resetForm();
    $('#reNewInfoForm').bootstrapValidator('validate');
    return $("#reNewInfoForm").data('bootstrapValidator').isValid();
};

/**
 * 提交添加
 */
ReNewInfoDlg.addSubmit = function() {

    this.clearData();
    this.collectData();

    if (!this.validate()) {
        return;
    }
    if (!this.canSubmit) {
        return;
    }
    this.canSubmit = false;
    //提交信息
    var ajax = new $ax(Feng.ctxPath + "/policy/reNew", function(data){
        Feng.success("添加成功!");
        window.parent.Policy.table.refresh();
        ReNewInfoDlg.canSubmit = true;
        ReNewInfoDlg.close();
    },function(data){
        setTimeout(function () {
            ReNewInfoDlg.canSubmit = true;
        },3000);
        Feng.error("添加失败!" + data.responseJSON.message + "!");
    });
    ajax.set(this.reNewInfoData);
    ajax.start();
}

$(function() {
    Feng.initValidator("reNewInfoForm", ReNewInfoDlg.validateFields);
    $("#insuranceId").val(null);
});
